package com.gulimall.product.app;

import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.R;
import com.gulimall.product.entity.ProductAttrValueEntity;
import com.gulimall.product.service.AttrService;
import com.gulimall.product.service.ProductAttrValueService;
import com.gulimall.product.vo.AttrRespVo;
import com.gulimall.product.vo.AttrVO;
import com.gulimall.product.vo.ProductAttrUpdateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 商品属性
 *
 * @author kkk
 * @email 2017809755@qq.com
 * @date 2022-05-17 21:49:10
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    //sale/list/{catId}
//    @GetMapping("/base/list/{catId}")
    @GetMapping("/{type}/list/{catId}")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Integer catId, @PathVariable String type) {
        PageUtils page = attrService.queryPage(params, catId, type);

        return R.ok().put("page", page);
    }

    /**
     * 获得商品属性规格
     *
     * @param spuId
     * @return
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R getListForSpu(@PathVariable Long spuId) {
        List<ProductAttrValueEntity> attrList = productAttrValueService.getListForSpu(spuId);

        return R.ok().put("data", attrList);
    }

    @PostMapping("/update/{spuId}")
    public R updateProductAttr(@RequestBody List<ProductAttrUpdateVo> productAttrUpdateVos, @PathVariable Long spuId) {
        productAttrValueService.updateProductAttr(productAttrUpdateVos,spuId);

        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
//        AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attrRespVo = attrService.getAttrResp(attrId);

        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVO attr) {
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrRespVo attr) {
//        attrService.updateById(attr);
        attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
